package com.jin.webvideo.util.base;

import com.jin.webvideo.util.cloneUtil.CloneUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class MapBuilder<T, V> {
    private Stack<Map<T,V>> stack = new Stack<>();
    public MapBuilder(Map<T,V> map){
        stack.push(map);
    }
    public MapBuilder(){
        this(new HashMap<>());
    }
    public MapBuilder<T,V> put(T key, V val){
        stack.peek().put(key, val);
        return this;
    }
    public MapBuilder<T,V> putAll(Map<T,V> map){
        stack.peek().putAll(map);
        return this;
    }
    public MapBuilder<T,V> save(){
        // 原map不处理保存现场， 新的入栈
        stack.push(build());
        return this;
    }
    public MapBuilder<T,V> restore(){
        // 顶层出栈，恢复现场
        stack.pop();
        return this;
    }
    // 返回的是原来的副本
    public Map<T, V> build(){
        return CloneUtil.clone(stack.peek());
    }
}
